﻿using Mirror;
using System;
using UnityEngine;

namespace NetTest
{
    public class ShotPlayer:NetworkBehaviour
    {
        public Rigidbody RB;

        [SerializeField] private float _moveSpeed;
        
        [SerializeField]private GameObject _bulletPrefab;
        [SerializeField]private Transform _shotPos;

        private void Update()
        {
            if (!isLocalPlayer) { return; }
            Move(new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")));

            if (Input.GetKeyDown(KeyCode.Space)) 
            {
                Shot(transform.forward,10);
            }
        }

        [Command]
        private void Shot(Vector3 dir, float speed)
        {
            var bullet=Instantiate(_bulletPrefab,_shotPos.position,_shotPos.rotation);
            var projectile=bullet.GetComponent<Projectile>();
            NetworkServer.Spawn(bullet);
            projectile.Shot(transform.forward, 10);
        }

      

        private void Move(Vector3 direction)
        {
            var dir = direction.normalized;
            var targetPosition = RB.position + _moveSpeed * dir * Time.deltaTime;
            var targetVelocity = (targetPosition - transform.position) / Time.deltaTime;
            RB.velocity = new Vector3(targetVelocity.x,RB.velocity.y,targetVelocity.z);
        }
    }
}
